<template>
    <view class="container">
        <view class="form-box">
            <view class="form-item">
                <input class="username" :value="username" @input="bindUsernameInput" placeholder="账号" />
                <van-icon name="close" v-if="username.length > 0" id="clear-username" class="clear" @tap.native.stop.prevent="clearInput" />
            </view>

            <view class="form-item">
                <input class="password" :value="password" password @input="bindPasswordInput" placeholder="密码" />
                <van-icon class="clear" id="clear-password" v-if="password.length > 0" name="close" @tap.native.stop.prevent="clearInput" />
            </view>

            <!-- <view class="form-item-code" wx-if="{{loginErrorCount >= 3}}">
			<view class="form-item code-item">
				<input class="code" value="{{code}}" bindinput="bindCodeInput" placeholder="验证码"/>
        <van-icon class="clear" id="clear-code" wx:if="{{ code.length > 0 }}" name="close" catchtap="clearInput"/>        
			</view>
			<image class="code-img" src="captcha.png"></image>
		</view>  -->

            <button type="primary" class="login-btn" @tap="accountLogin">账号登录</button>

            <view class="form-item-text">
                <navigator url="/pages/auth/register/register" class="register">注册账号</navigator>
                <navigator url="/pages/auth/reset/reset" class="reset">忘记密码</navigator>
            </view>
        </view>
    </view>
</template>

<script>
var api = require('../../../config/api.js');

var util = require('../../../utils/util.js');

var user = require('../../../utils/user.js');

var app = getApp();
export default {
    data() {
        return {
            username: {
                length: 0
            },
            password: {
                length: 0
            },
            code: '',
            loginErrorCount: 0
        };
    },
    onLoad: function (options) {
        // 页面初始化 options为页面跳转所带来的参数
        // 页面渲染完成
    },
    onReady: function () {},
    onShow: function () {
        // 页面显示
    },
    onHide: function () {
        // 页面隐藏
    },
    onUnload: function () {
        // 页面关闭
    },
    methods: {
        accountLogin: function () {
            var that = this;

            if (this.password.length < 1 || this.username.length < 1) {
                uni.showModal({
                    title: '错误信息',
                    content: '请输入用户名和密码',
                    showCancel: false
                });
                return false;
            }

            uni.request({
                url: api.AuthLoginByAccount,
                data: {
                    username: that.username,
                    password: that.password
                },
                method: 'POST',
                header: {
                    'content-type': 'application/json'
                },
                success: function (res) {
                    if (res.data.errno == 0) {
                        that.setData({
                            loginErrorCount: 0
                        });
                        app.globalData.hasLogin = true;
                        uni.setStorageSync('userInfo', res.data.data.userInfo);
                        uni.setStorage({
                            key: 'token',
                            data: res.data.data.token,
                            success: function () {
                                uni.switchTab({
                                    url: '/pages/ucenter/index/index'
                                });
                            }
                        });
                    } else {
                        that.setData({
                            loginErrorCount: that.loginErrorCount + 1
                        });
                        app.globalData.hasLogin = false;
                        util.showErrorToast('账户登录失败');
                    }
                }
            });
        },

        bindUsernameInput: function (e) {
            this.setData({
                username: e.detail.value
            });
        },

        bindPasswordInput: function (e) {
            this.setData({
                password: e.detail.value
            });
        },

        bindCodeInput: function (e) {
            this.setData({
                code: e.detail.value
            });
        },

        clearInput: function (e) {
            switch (e.currentTarget.id) {
                case 'clear-username':
                    this.setData({
                        username: ''
                    });
                    break;

                case 'clear-password':
                    this.setData({
                        password: ''
                    });
                    break;

                case 'clear-code':
                    this.setData({
                        code: ''
                    });
                    break;
            }
        }
    }
};
</script>
<style>
@import './accountLogin.css';
</style>
